package com.ggb.reactive.controller.admin

import com.ggb.reactive.constant.MessageConstant
import com.ggb.reactive.model.WebResult
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import lombok.RequiredArgsConstructor
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@RequiredArgsConstructor
@Tag(name = "店铺管理")
class ShopController(
    val reactiveRedisTemplate: ReactiveRedisTemplate<String, Any>
) {
    private val SHOP_KEY = "shop_key"

    @PutMapping("/{status}")
    @Operation(summary = "设置营业状态")
    fun setStatus(@PathVariable status:Int): Mono<WebResult<String>> {
        return reactiveRedisTemplate.opsForValue().set(SHOP_KEY, status)
            .then(Mono.just(WebResult.success(MessageConstant.ACTION_SUCCESS)))
    }

    @GetMapping("/status")
    @Operation(summary = "获取店铺状态")
    fun getStatus(): Mono<WebResult<Int>> {
        return reactiveRedisTemplate.opsForValue().get(SHOP_KEY)
            .map {
                it ?: 0
            } // 安全地转换为 Int，如果为 null 或转换失败则默认为 0
            .map { WebResult.success(it as Int) }
    }
}